<?php
/**
 * Created by PhpStorm.
 * User: wchuang
 * Date: 2016/8/26
 * Time: 10:30
 */

namespace common\queue;

use \Resque;

class Queue extends Resque
{
	const REDIS_SERVER      = 'localhost:6379';
	const REDIS_DATABASE    = 0;

	const PREFIX            =   '';         //redis 命名空间
	const PROCESS_COUNT     =   1;          //启动进程数量
	const LOG_LEVEL         =   2;          //0=none,1=normal,2=detail
	const WORK_CHECK_TIME   =   3;          //每n秒检查队列并处理
	const APP_INCLUDE       =   '';         //job class目录，有composer自动加载时候，则无需

	public static $queueObject = '*';       //要处理的队列

	public function __construct($queueObject = '*')
	{
		self::$queueObject = $queueObject;
	}

	public function start(){
		/**
		 * PREFIX
		 * COUNT
		 * QUEUE
		 * LOGGING  :log level 1 => normal
		 * VVERBOSE :log level 2 => detail
		 */
		if(self::LOG_LEVEL == 1){
			putenv("LOGGING=true");
		}else if(self::LOG_LEVEL == 2){
			putenv("VVERBOSE=true");
		}
		putenv("QUEUE=" . self::$queueObject);
		putenv("COUNT=" . self::PROCESS_COUNT);
		putenv("APP_INCLUDE=" . self::APP_INCLUDE);
		putenv("INTERVAL=" . self::WORK_CHECK_TIME);

		putenv("REDIS_BACKEND=" . self::REDIS_SERVER);


		$path = \Yii::$app->vendorPath . '/' . "chrisboulton/php-resque/resque.php";
		include $path;

	}

	/**
	 * 创建一个任务
	 * @param $job
	 * @param $args
	 * @param string $queueType
	 */
	public static function newQueue($job,$args,$queueType = 'default'){

		Queue::enqueue($queueType,'common\queue' . "\\" . $job ,$args);
	}

	public function test(){
		$args=array(
			'name'=>'Chris'
		);

		Queue::enqueue('default','common\queue\My_Job',$args);
	}
}